package com.xiaoxu.intranetweb.mybatisplusSzappdb.timerTasks;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.TCompete;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.mapper.TCompeteMapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ITCompeteService;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class TimeListener {

    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private ITCompeteService iTCompeteService;

    @Autowired
    private TCompeteMapper tCompeteMapper;
//只有在邹县不使用
//    @Scheduled(cron = "0 0/1 * * * ?") // 每分钟执行一次,
//    public void listenTimeAndSetStatus() throws Exception {
//        // 查询TCompete表的beginDate时间
//        // 实现查询TCompete表的beginDate时间的方法
//        QueryWrapper<TCompete> queryWrapper = new QueryWrapper<>();
//        queryWrapper.select("BEGIN_DATE", "ID").lt("BEGIN_DATE", LocalDateTime.now()).eq("DEL_FLAG", 0).lt("STATUS", 31);
//        ;
//        // 返回查询到的beginDate时间
//        List<TCompete> tCompeteList = tCompeteMapper.selectList(queryWrapper);
//        // 获取查询到的beginDate时间
//
//        List<Pair<String, LocalDateTime>> list = tCompeteList.stream()
//                .map(tCompete -> Pair.of(tCompete.getId(), tCompete.getBeginDate()))
//                .collect(Collectors.toList());
//
//        // 如果当前时间达到了beginDate的时间，将TCompete表的状态设置为开标
//        for (Pair<String, LocalDateTime> stringLocalDateTimePair : list) {
//            String left = stringLocalDateTimePair.getLeft();
//            QueryWrapper<TCompete> queryWrapper1 = new QueryWrapper<>();
//            queryWrapper1.eq("ID", left);
//            queryWrapper1.eq("DEL_FLAG", 0);
//            TCompete one = iTCompeteService.getOne(queryWrapper1);
//            if (one != null) {
//                one.setStatus(31);
//                iTCompeteService.updateById(one);
//            }
//        }
//    }
//排队序号的更新
//    @Scheduled(cron = "0 0/1 * * * ?") // 每分钟执行一次
}